package cn.swing.ushare.element.webapi.service;

import cn.swing.ushare.element.service.IElementService;
import cn.swing.ushare.element.webapi.mapper.ElementMapper;
import cn.swing.ushare.pojo.dto.CarouselDTO;
import cn.swing.ushare.pojo.dto.MenuBarDTO;
import cn.swing.ushare.pojo.vo.CarouselVO;
import cn.swing.ushare.pojo.vo.MenuBarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ElementServiceImpl implements IElementService {

    @Autowired
    private ElementMapper mapper;

    @Override
    public List<CarouselVO> list() {
        return mapper.list();
    }

    @Override
    public Integer updateCarousel(CarouselVO carousel) {
        mapper.insertOperationLog("轮播图管理","更新",logOperation(),"更新了轮播图' "+carousel.getName()+"'的信息");
        return mapper.updateCarousel(carousel);
    }

    @Override
    public Integer deleteCarousel(Integer id) {
        mapper.insertOperationLog("轮播图管理","删除",logOperation(),"删除了名为'" + mapper.selectCarouselNameById(id) + "'的轮播图");
        return mapper.deleteCarousel(id);
    }

    @Override
    public Integer insertCarousel(CarouselDTO carousel) {
        mapper.insertOperationLog("轮播图管理","插入",logOperation(),"插入了名为' "+carousel.getName()+"'的轮播图");
        return mapper.insertCarousel(carousel);
    }

    @Override
    public List<MenuBarVO> listMenuBar() {
        return mapper.listMenuBar();
    }

    @Override
    public Integer updateMenuBar(MenuBarDTO menuBar) {
        mapper.insertOperationLog("导航栏管理","更新",logOperation(),"更新了导航栏菜单' "+menuBar.getName()+"'的信息");
        return mapper.updateMenuBar(menuBar);
    }

    @Override
    public Integer deleteMenuBar(Integer id) {
        mapper.insertOperationLog("导航栏管理","删除",logOperation(),"删除了名为'" + mapper.selectMenuBarNameById(id) + "'的导航栏菜单");
        return mapper.deleteMenuBar(id);
    }

    @Override
    public Integer insertMenuBar(MenuBarDTO menuBarDTO) {
        mapper.insertOperationLog("导航栏管理","插入",logOperation(),"插入了名为' "+menuBarDTO.getName()+"'的导航栏菜单");
        return mapper.insertMenuBar(menuBarDTO);
    }

    // 日志操作
    private String logOperation(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = (String) principal;
        return username;
    }
}
